<?php
namespace app\common\service\impl;
use app\common\service\DispatchService;
use app\common\model\Dispatch;
use app\common\model\DispatchLog;
use think\facade\Log;
class DispatchServiceImpl implements DispatchService
{
    
    protected $dispatch;
    protected $dispatchLog;
    
    public function __construct(Dispatch $dispatch,DispatchLog $dispatchLog)
    {
       $this->dispatch = $dispatch;
       $this->dispatchLog = $dispatchLog;
    }
    
    public function edit(array $data)
    {
        $value = $this->dispatch->where(['id'=>$data['id']])->value($data['key']);
        
        $dateArr = ['apply_time','deliver_time','depart_time'];
        if(in_array($data['key'],$dateArr)){
            $value = date('Y-m-d',$value);
        }
        
        $this->dispatch->startTrans();
        try{
            
            $this->dispatch->save([$data['key']=>$data['value']],['id'=>$data['id']]);
            
            if($this->dispatch->update_time){
                $model = $this->recordLog($data,$value,$this->dispatch[$data['key']]);
               
                if($model->id){
                    $this->dispatch->commit();
                    return $this->dispatch;
                }else{
                    $this->dispatch->rollback();
                    return false;
                }
                
            }else{
                $this->dispatch->rollback();
                return false;
            }
            
        }catch(\Exception $e){
            $this->dispatch->rollback();
            Log::record('[ 调拨记录更新数据 ]:'.$e->getMessage(),'error');
            return false;
        }
    }
    
    public function recordLog($data,$oldValue,$value){
        $type = [
            'depart'=>1,
            'apply_time'=>2,
            'receive_tel'=>3,
            'deliver_time'=>4,
            'depart_time'=>5,
            'receive'=>6,
            'receive_name'=>7,
            'address'=>8
        ];
        
        $log = [];
        $log = ['dispatch_id'=>$data['id'],'admin_id'=>session('admin.id'),'type'=>$type[$data['key']],'old_value'=>$oldValue,'value'=>$value];
        
        try {
            
            $model = $this->dispatchLog->create($log);
            
        } catch (\Exception $e) {
            Log::record('[ 记录日志 ]'.$e->getMessage(),'error');
            return false;
        }
        
        return $model; 
    }
    public function dispatchLog(array $data)
    {
        try {
            $model = $this->dispatchLog::with('admin')->where(['dispatch_id'=>$data['id']])->order('id', 'desc')
            ->paginate($data['length'],false,[
                'type'     => 'bootstrap',
                'var_page' => 'page',
                'page'=>$data['page'],
            ]);
        } catch (\Exception $e) {
            Log::record('[ 查询日志 ]'.$e->getMessage(),'error');
            return false;
        }
        
        return $model;
    }
    
    public function delete(int $id)
    {
    
        try {
            $this->dispatch->destroy($id);
        } catch (\Exception $e) {
            Log::record('[ 删除数据 ]:'.$e->getMessage(),'error');
            return false;
        }
        return true;
    }


        
}

